<?php
namespace Home\Controller;
use Think\Controller;
use Common\Model\UserModel;
use Common\Controller\CommonController;
class LoginController extends CommonController {
    public function __construct()
    {
        parent::__construct();
        $this->return_type='ajax';
    }
    public function index()
    {
        $this->display();
    }
    public function checkLogin()
    {
        $input=I('post.');
        $this->notEmptyValidate($input['username'],'用户名不能为空');
        $this->notEmptyValidate($input['password'],'密码不能为空');
        $this->notEmptyValidate($input['code'],'验证码不能为空');
        
        $this->trueValidate($this->checkVierify($input['code'],'login'),'验证码不正确');
        
        $model=UserModel::newSelf();
        $user=$model->findUserByUsername($input['username']);
        $this->notEmptyValidate($user,'用户名不存在');
        
        $res=$model->checkUserPassword($input['password'], $user['password']);
        $this->trueValidate($res,'用户名或密码不正确');
        
        $_SESSION['USER_KEY_ID']=$user['id'];
        
        $this->returnResult('登录成功',true);
    }
    public function logout()
    {
        unset($_SESSION["USER_KEY_ID"]);
        $this->redirect('Login/index');
    }
}